# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=testtools
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.5.0
pkgrel=3
pkgdesc="Extensions to the Python standard library unit testing framework (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://github.com/testing-cabal/testtools'
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python-extras"
         "${MINGW_PACKAGE_PREFIX}-python-fixtures"
         "${MINGW_PACKAGE_PREFIX}-python-pbr")
makedepends=('git' 
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
options=('staticlibs' 'strip' '!debug')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/testing-cabal/testtools/archive/${pkgver}.tar.gz")
sha256sums=('5b332ceec9a893303647a152d1217653e47f7ab2c32d6a9eb16bdbce57f93757')

prepare() {
  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"

  # These are for python 2.x and old python 3.x only
  sed -i 's|unittest2|unittest|' python-build-${CARCH}/testtools/tests/test_run.py \
                                 python-build-${CARCH}/testtools/tests/test_testsuite.py \
                                 python-build-${CARCH}/testtools/tests/twistedsupport/test_matchers.py \
                                 python-build-${CARCH}/testtools/tests/twistedsupport/test_deferred.py \
                                 python-build-${CARCH}/testtools/tests/twistedsupport/test_runtest.py
  sed -i '/unittest2>=1.0.0/d' python-build-${CARCH}/requirements.txt
  sed -i 's|import linecache2 as linecache|import linecache|' python-build-${CARCH}/testtools/tests/test_compat.py
  sed -i "s|traceback = try_import('traceback2')|import traceback|" python-build-${CARCH}/testtools/content.py
  sed -i '/traceback2/d' python-build-${CARCH}/requirements.txt

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
  export PBR_VERSION=${pkgver}
}

build() {
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python -m testtools.run testtools.tests.test_suite
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
